<?php

/**
 * category actions.
 *
 * @package    maximous
 * @subpackage category
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class categoryActions extends MaxControllerActions
{

    public function preExecute()
    {
        parent::preExecute();
        $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::Category);
    }

    function executeIndex(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canView());
        $this->formFilter = new CategoryFormFilter(array(),array('maxUser'=>  $this->maxUser));
//        echo $this->formFilter->getWidgetSchema()->getNameFormat();
        $this->formFilter->bind($request->getParameter($this->formFilter->getName()));
        $query = $this->formFilter->buildQuery($this->formFilter->getValues());
        $page  = $request->getParameter('page', 1);
        $this->pager = $this->createPager('Category', $query, $page, sfConfig::get('app_table_list_row_numbers'));
    }

    public function executeNew()
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->form = new CategoryForm(null, array('i18n' => $this->i18n,'maxUser'=>  $this->maxUser));
    }

    public function executeCreate(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canAdd());
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->form = new CategoryForm(null, array('i18n' => $this->i18n,'maxUser'=>  $this->maxUser));

        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

        if ($this->form->isValid())
        {
            $this->form->save();
            $this->getUser()->setFlash('message_success', $this->i18n->__('add category successful'));
            $this->redirect('category/index');
        }
        else
        {
            $this->getUser()->setFlash('message_failed', $this->i18n->__('add category failed'));
        }
        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canEdit());
        $this->forward404Unless($category = Doctrine_Core::getTable('Category')->find(array($request->getParameter('id'))), sprintf('Object category does not exist (%s).', $request->getParameter('id')));
        $this->form = new CategoryForm($category, array('i18n' => $this->i18n,'maxUser'=>  $this->maxUser));
    }

    public function executeUpdate(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canEdit());
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($category = Doctrine_Core::getTable('Category')->find(array($request->getParameter('id'))), sprintf('Object category does not exist (%s).', $request->getParameter('id')));
        $this->form = new CategoryForm($category, array('i18n' => $this->i18n,'maxUser'=>  $this->maxUser));

        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));

        if ($this->form->isValid())
        {
            $category = $this->form->save();
            $this->getUser()->setFlash('message_success', $this->i18n->__('edit category') . $category->getTitle() . $this->i18n->__('successful'));
        }
        else
        {
            $this->getUser()->setFlash('message_failed', $this->i18n->__('edit category') . $category->getTitle() . $this->i18n->__('failed'));
        }
        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        //check access
        $this->checkAccess($this->maxUser->getMaxUserRole()->canDelete());
        $this->forward404Unless($category = Doctrine_Core::getTable('Category')->find(array($request->getParameter('id'))), sprintf('Object category does not exist (%s).', $request->getParameter('id')));
        if ($category->getJobsNumber() > 0)
        {
            $this->getUser()->setFlash('message_failed', $this->i18n->__('can\'t delete category') . ' ' . $category->getTitle() . ' ' . $this->i18n->__('content') . ' ' . $category->getJobsNumber() . ' ' . $this->i18n->__('jobs'));
        }
        else
        {
            $this->getUser()->setFlash('message_success', $this->i18n->__('delete category' . ' ' . $category->getTitle() . ' ' . $this->i18n->__('done')));
            $category->delete();
        }

        $this->redirect('category/index');
    }

}
